/**
 * @ Author: luoqi
 * @ Create Time: 2024-09-12 11:28
 * @ Modified by: luoqi
 * @ Modified time: 2025-03-10 23:07
 * @ Description:
 */

#include <bsp.h>
#include "button.h"
#include "../modules/qbutton.h"
#include "../qshell/qterm.h"

static QButton btn_1;
static QButton btn_2;

static TX_THREAD task_button;
static uint8_t task_button_stack[256];

static int btn_1_read(void)
{
    return HAL_GPIO_ReadPin(BTN1_GPIO_Port, BTN1_Pin);
}

static int btn_2_read(void)
{
    return HAL_GPIO_ReadPin(BTN2_GPIO_Port, BTN2_Pin);
}

static int btn_1_click_cb(int kv)
{
    UNUSED(kv);
    QSH(" btn1 click\r\n");
    return 0;
}

static int btn_2_click_cb(int kv)
{
    UNUSED(kv);
    QSH(" btn2 click\r\n");
    return 0;
}

static void _task_button_hdl(uint32_t arg)
{
    UNUSED(arg);
    while(1) {
        qbutton_exec(&btn_1);
        qbutton_exec(&btn_2);
        tx_thread_sleep(10);
    }
}

int button_init(void)
{
    qbutton_init(&btn_1, QBUTTON_PRESS_DOWN_KEYVAL_LOW, 2, 120, 35, btn_1_read);
    qbutton_init(&btn_2, QBUTTON_PRESS_DOWN_KEYVAL_LOW, 2, 120, 35, btn_2_read);

    qbutton_events_attach(&btn_1, QBUTTON_ACTION_SINGLE_CLICK, btn_1_click_cb);
    qbutton_events_attach(&btn_2, QBUTTON_ACTION_SINGLE_CLICK, btn_2_click_cb);


    tx_thread_create(&task_button, "button", _task_button_hdl, 0,
        task_button_stack, sizeof(task_button_stack), 
        10, 10, TX_NO_TIME_SLICE, TX_AUTO_START);

    return 0;
}
